package org.example.producer;

import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import org.example.model.User;
import org.example.util.JSONUtils;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class EventProducer {

    //加入了spring-kafka依赖 + .yml配置信息，springboot自动配置好了kafka，自动装配好了KafkaTemplate这个Bean
    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendEvent() throws JsonProcessingException {
        User user = User.builder()
                .id(1028)
                .phone("1235324234312")
                .birthDay(new Date())
                .build();
        String userJSON = JSONUtils.toJSON(user);
        kafkaTemplate.send("topicA", "k", userJSON);
    }

}
